package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.mapper.GoodsMapper;
import com.ruoyi.pojo.Goods;
import com.ruoyi.service.DubboDemoService;
import com.ruoyi.service.GoodsService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.List;

//@Service
@DubboService // 暴露服务  通过这个配置可以基于 Spring Boot 去发布 Dubbo 服务。
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public List<Goods> getGoodsList() {
        return goodsMapper.selectList(null);
    }

    @Override
    public boolean insertGoods(Goods goods) {
        int insert = goodsMapper.insert(goods);
        System.err.println("我是商品插入，状态-------"+insert);
        if(insert == 1){
            return true;
        }
        return false;
    }

    @Override
    public int updateGoods(Goods goods) {
        //根据商品id获取商品信息
        Goods goods1 = goodsMapper.selectById(goods.getId());
        //商品库存小于0或者库存小于购买数量
        if(goods1.getGoodsCount() < 0 || goods1.getGoodsCount() < goods.getGoodsCount()){
            throw new RuntimeException("商品库存不足，剩余数量为："+goods1.getGoodsCount());
        }
        int i = 10/0;
        System.err.println(i);
        //更新商品库存信息
        goods1.setGoodsCount(goods1.getGoodsSum() - goods.getGoodsCount());
        int update = goodsMapper.updateById(goods1);
        return update;
    }

    @Override
    public String sayHello(String name) {
        return "我是Goods服务，你好" + name ;
    }
}
